package com.csb.controller;

import com.csb.config.ErrorForbiddenException;
import com.csb.config.ErrorRegisterException;
import com.csb.config.MyConstants;
import com.csb.pojo.UserInfo;
import com.csb.service.UserService;
import com.csb.util.MD5;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/user")
public class UserController {
    // 依赖
    @Resource
    private UserService userService;

    @RequestMapping("/login")
    public String login(UserInfo userInfo) {
        String rtn = "";
        String pwd = MD5.encrypt(userInfo.getPwd());
        UsernamePasswordToken token = new UsernamePasswordToken(userInfo.getAccount(),pwd);
        // System.out.println(token);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            // 修改登陆时间
            userService.updateUserCreationTime(userInfo.getAccount());
            rtn = "1";
        } catch (UnknownAccountException e) {
            rtn="-1";
            e.printStackTrace();
        }catch (IncorrectCredentialsException e) {
            rtn="0";
            e.printStackTrace();
        }
        // 账号注册未通过异常
        catch (ErrorRegisterException e){
            rtn="-2";
            e.printStackTrace();
        }
        // 账号禁用异常
        catch (ErrorForbiddenException e){
            rtn="-3";
            e.printStackTrace();
        }
        return rtn;
    }

    /**
     * 验证账号是否重复
     * @param account
     * @return
     */
    @RequestMapping("/chkAccount")
    public String chkAccount(String account){
        if (userService.chkAccount(account)){
            return "1";
        }else {
            return "0";
        }
    }
    /**
     * 申请账号
     * @param userInfo
     * @return
     */
    @RequestMapping("/add")
    public String addUserInfo(UserInfo userInfo){
        // 判断
        if (userService.addUserInfo(userInfo)){
            return "1";
        }else {
            return "0";
        }
    }

    /**
     * 账号申请一览
     * @return
     */
    @RequestMapping("/noUserList")
    public List<UserInfo> noUserList(){
        // 获取账号申请列表
        return userService.getAroUserList();
    }

    /**
     * 同意账号的申请
     * @param ids 账号的id
     * @return
     */
    @RequestMapping("/userAroYes")
    public String userAroYes(Integer[] ids){
        if (userService.userAroYes(ids)) {
            return "1";
        } else {
            return "0";
        }
    }

    /**
     * 不同意账号的申请
     * @param ids 账号的id
     * @return
     */
    @RequestMapping("/userAroNo")
    public String userAroNo(Integer[] ids){
        if (userService.userAroNo(ids)) {
            return "1";
        } else {
            return "0";
        }
    }

    /**
     * 获取所有账号列表
     * @return
     */
    @RequestMapping("/allUserInfo")
    public List<UserInfo> findAllUserInfo(){
        List<UserInfo> u = userService.findAllUserInfo();
        // 获取所有账号列表
        return u;
    }

    /**
     * 禁用或者启用账号
     * @param id
     * @param isShow
     * @return
     */
    @RequestMapping("/userShowYesOrNo")
    public String updateUserShowYesOrNo(Integer id,Integer isShow,String rName){
        return userService.updateUserShowYesOrNo(id,isShow,rName);
    }

    /**
     * 获取个人账号信息
     * @return
     */
    @RequestMapping("/findAccountInfo")
    public UserInfo findAccountInfo(){
        // 获取账号
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        if (userInfo == null){
            return userInfo;
        }
        UserInfo user = userService.getUserInfoByAccount(userInfo.getAccount());
        // 获取账号信息
        return user;
    }

    /**
     * 根据账号修改信息
     * @param userInfo
     * @return
     */
    @RequestMapping("/updateUserInfo")
    public String updateUserInfo(UserInfo userInfo){
        if (userService.updateUserInfo(userInfo)){
            return "1";
        }else {
            return "0";
        }
    }

    /**
     * 修改密码
     * @param userInfo
     * @return
     */
    @RequestMapping("/updateUserPwd")
    public String updateUserPwd(UserInfo userInfo,String oldPwd){
        // 获取账号
        UserInfo user = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        if (oldPwd.equals(MD5.encrypt(user.getPwd()))){
            if (userService.updateUserPwd(userInfo)){
                return "1";
            }else {
                return "0";
            }
        }else {
            return "-1";
        }
    }

    /**
     * 修改用户权限
     * @param id
     * @return
     */
    @RequestMapping("/managerYes")
    public String updateManagerYes(Integer id,String rName){
        // 获取账号
        if (MyConstants.ROLE_ADMIN.equals(rName)){
            return "-1";
        }else if (userService.updateManagerYes(id)){
            return "1";
        }else {
            return "0";
        }
    }
    /**
     * 修改用户权限
     * @param id
     * @return
     */
    @RequestMapping("/managerNo")
    public String updateManagerNo(Integer id,String rName){
        if (MyConstants.ROLE_ADMIN.equals(rName)){
            return "-1";
        }else if (userService.updateManagerNo(id)){
            return "1";
        }else {
            return "0";
        }
    }

    /**
     * 修改用户权限
     * @param id
     * @return
     */
    @RequestMapping("/del")
    public String deleteUserInfo(Integer id,String account,String rName){
        // 获取账号
        UserInfo user = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        if (MyConstants.ROLE_ADMIN.equals(rName)){
            return "0";
        }else if(MyConstants.ROLE_ADMIN.equals(user.getRolesList().getRName())){
            if (!userService.userUseAssetCount(account)){
                if (userService.deleteUserInfo(id)){
                    return "1";
                }
                return "-1";
            }
            return "-2";
        } else if (MyConstants.ROLE_MANAGER.equals(rName)){
            return "0";
        }else if (!userService.userUseAssetCount(account)){
            if (userService.deleteUserInfo(id)){
                return "1";
            }
            return "-1";
        }else {
            return "-2";
        }
    }
    /**
     * 修改用户头像
     * @param
     * @return
     */
    @RequestMapping("updateHeadPortrait")
    public String updateHeadPortrait(@RequestParam(value = "account") String account , @RequestParam(value = "file") MultipartFile multipartFile){
        try {
            int i = userService.updateHeadProtrait(multipartFile.getBytes(),account);
            return "success";
        } catch (IOException e) {
            e.printStackTrace();
            return "error";
        }

    }
}
